import 'package:flutter/material.dart';
import 'package:woandroid/pages/BaseState.dart';
import 'package:woandroid/constant/Costants.dart';
import 'package:woandroid/view/EndLine.dart';
import 'package:woandroid/http/Api.dart';
import 'package:woandroid/http/HttpUtilWithCookie.dart';

import 'package:woandroid/itemView/ArticleItem.dart';
class ArticleListPage extends StatefulWidget{

  String id;
  ArticleListPage(this.id);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new ArticleListPageState();
  }
}
class ArticleListPageState extends BaseState<ArticleListPage> with AutomaticKeepAliveClientMixin{

  @override
  bool get wantKeepAlive => true;

  int curPage=0;
  Map<String ,String> map=new Map();
  List listData=new List();

  int listTotalSize=0;
  ScrollController controller=new ScrollController();

  @override
  void initState() {
    // TODO: implement initState


    super.initState();

    _getArticleList();

    controller.addListener((){

      var maxScroll = controller.position.maxScrollExtent;
      var pixels = controller.position.pixels;

      if (maxScroll == pixels && listData.length < listTotalSize) {
        _getArticleList();
      }
    });
  }

  @override
  void dispose() {

    controller.dispose();
    // TODO: implement dispose
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {

    if(listData==null||listData.isEmpty){

      return new Center(child: new CircularProgressIndicator(),);

    }else{
      Widget lv=ListView.builder(itemBuilder: (context,i){
        return builditem(i);
      },itemCount: listData.length,controller: controller,key: new PageStorageKey(widget.id),);
      return lv;
    }

  }

  void _getArticleList() {
    String url = Api.ARTICLE_LIST;
    url += "$curPage/json";
    map['cid'] = widget.id;
    HttpUtil.get(url, (data) {
      if (data != null) {
        Map<String, dynamic> map = data;

        var _listData = map['datas'];
        listTotalSize = map["total"];
        setState(() {
          List list1 = new List();
          if (curPage == 0) {
            listData.clear();
          }
          curPage++;

          list1.addAll(listData);
          list1.addAll(_listData);
          if (list1.length >= listTotalSize) {
            list1.add(Constants.END_LINE_TAG);
          }
          listData = list1;
        });
      }
    }, params: map);


  }

  Future<Null> _pullToRefresh() async {
    curPage = 0;
    _getArticleList();
    return null;
  }

  Widget builditem(int i) {
    var itemData=listData[i];
    if(i==listData.length-1&&itemData.toString()==Constants.END_LINE_TAG){
      return new EndLine();
    }
    return new ArticleItem(itemData);
  }
}